#include <iostream>
#include <map>

using namespace std;

typedef long long LL;

int n, p;
map<string, int> mp; // <高级词汇，含金量>

// 判断 ch 是否合法
bool check(char ch)
{
    if((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    {
        return true;
    }
    else return false;
}

int main()
{
    cin >> n >> p;
    for(int i = 1; i <= n; i++)
    {
        string s; int x;
        cin >> s >> x;
        mp[s] = x; 
    }

    LL ret = 0;
    char ch; // 一个字符一个字符读
    string t = "";

    while(scanf("%c", &ch) != EOF)
    {
        if(check(ch)) t += ch;
        else
        {
            // 读到分隔符
            ret = (ret + mp[t]) % p;
            t = "";
        }
    }

    cout << ret << endl;

    return 0;
}